/*
 * <p>Copyright® 2009-2022 东软 IT 实训中心版权所有。</p>
 */

package com.max.back.doctor.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author 辛凤文 forward.seen@foxmail.com
 * @version 1.0
 * @description <p><code>RedisUtils</code>
 * @time 2022/7/17 23:21
 * @see
 */
@Component
public class RedisUtils {

    /**
     * 获取到Redis模板
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置key、value
     * 注意事项：对Redis的key一定要设置一个有效期，否则就变成了持久化数据
     */
    public void setString(String key, String value, Long timeOut) {
        redisTemplate.opsForValue().set(key, value);
        if (timeOut != null) {
            redisTemplate.expire(key, timeOut, TimeUnit.SECONDS);
        }
    }

    public void setString(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public String getString(String key,Long timeOut) {
        redisTemplate.expire(key,timeOut,TimeUnit.SECONDS);
        return (String) redisTemplate.opsForValue().get(key);
    }
}